package echat.future.cn.echat.home;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.netease.nim.avchatkit.AVChatKit;
import com.netease.nim.avchatkit.AVChatUserData;
import com.netease.nim.avchatkit.activity.AVChatActivity;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

import echat.future.cn.echat.R;
import echat.future.cn.echat.dialog.YuyueDialog;
import echat.future.cn.echat.dialog.YuyueTipDialog;
import echat.future.cn.echat.home.bean.AnchorEntity;
import echat.future.cn.echat.home.bean.Video;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.view.RechargeActivity;
import echat.future.cn.echat.netease.NeteaseLoginUtil;
import echat.future.cn.echat.video.domain.VideoData;
import lumingweihua.future.cn.baselibgxh.base.GiftFragment;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;
import lumingweihua.future.cn.baselibgxh.utils.SharedPreference;

/**
 * Author  zhusx
 * Email   327270607@qq.com
 * Create  2018/9/23 20:51
 */
public class AnchorHomeActivity extends AnchorHomeBaseActivity implements View.OnClickListener {
    public String id;
    private AnchorEntity data;
    View rl_gift;
    View view_mask;
    View rl_pay;
    Video currentVideo;
    TextView tv_coin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NeteaseLoginUtil neteaseLoginUtil = new NeteaseLoginUtil(MyApplication.getInstance().getPhone());
//            NeteaseLoginUtil neteaseLoginUtil = new NeteaseLoginUtil("15876542461");
        neteaseLoginUtil.loginNetease(AnchorHomeActivity.this);
    }

    @Override
    public void onLoadSuccess(String id, @NotNull AnchorEntity data) {
        this.id = id;
        this.data = data;
        rl_gift = findViewById(R.id.rl_gift);
        view_mask = findViewById(R.id.view_mask);
        rl_pay = findViewById(R.id.rl_pay);
        tv_coin = findViewById(R.id.tv_coin);
        findViewById(R.id.bt_pay).setOnClickListener(this);
        findViewById(R.id.view_mask).setOnClickListener(this);
        findViewById(R.id.iv_close).setOnClickListener(this);
        findViewById(R.id.tv_subscribe).setOnClickListener(this);
        findViewById(R.id.tv_video).setOnClickListener(this);
        findViewById(R.id.tv_giveGifts).setOnClickListener(this);
        GiftFragment giftFragment = new GiftFragment();
        giftFragment.setListener(id, new GiftFragment.GiftChangeListener() {
            @Override
            public void onSendGift(String id) {

            }

            @Override
            public void onDissmiss() {
                rl_gift.setVisibility(View.GONE);
            }
        });
        getSupportFragmentManager().beginTransaction().add(R.id.rl_gift, giftFragment).commit();
        if(MyApplication.getInstance().isZhubo){
            findViewById(R.id.ll_bottom).setVisibility(View.GONE);
        }
    }

    //视频列表点击
    @Override
    public void onItemPlayVideo(@NotNull Video video, String price) {
        new YesOrNoDialog(this, new YesOrNoDialogEntity("该视频需付费观看\n您确认付费麽?", "取消", "立即付费"),
                new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                    @Override
                    public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                        switch (yesOrNoType) {
                            case BtnOk:
                                currentVideo = video;
                                view_mask.setVisibility(View.VISIBLE);
                                rl_pay.setVisibility(View.VISIBLE);
                                tv_coin.setText("E币：" + price);
                                break;
                        }

                    }
                }).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //送礼物
            case R.id.tv_giveGifts:
                if (id.equals(UserHelper.getUserId())) {
                    _showToast("不能给自己送礼");
                    return;
                }
                rl_gift.setVisibility(View.VISIBLE);
                break;
            //与他视频
            case R.id.tv_video:
                if (id.equals(UserHelper.getUserId())) {
                    _showToast("不能和自己视频聊天");
                    return;
                }
                if (!"1".equals(data.getLast_status())) {
                    //未拨通提示 传主播头像
                    new YuyueTipDialog(v.getContext(), data.getAvatar(), new YuyueTipDialog.OnYuyueTipDialogClickListener() {
                        @Override
                        public void onYesClick() {
                            //预约 传主播id
                            new YuyueDialog(v.getContext(), id, new YuyueDialog.OnYuyueDialogClickListener() {
                                @Override
                                public void onYesClick() {

                                }
                            }).show();
                        }
                    }).show();
                } else {
                    String date = SharedPreference.getString(this, "freedate");
                    String dateNowStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

//                    if (data.getFirst_free() == 1 && !date.contains(dateNowStr)) {
//                        //限免
////                        SharedPreference.saveToSP(this, "freedate", date + "," + dateNowStr);
//                        AVChatUserData avChatUserData = new AVChatUserData(
//                                UserHelper.getUserId()
//                                , id
//                                , data.getAvatar()
//                                , data.getUsername()
//                                , "1"
//                                , true
//                                , data.getPrice()
//                                , 60);
//                        AVChatKit.outgoingCall(AnchorHomeActivity.this,
//                                data.getMobile(),
//                                data.getUsername(),
//                                AVChatType.VIDEO.getValue(),
//                                AVChatActivity.FROM_INTERNAL, avChatUserData);
//                    } else
                        if (MyApplication.getInstance().e_money < data.getPrice()) {
                        new YesOrNoDialog(this, new YesOrNoDialogEntity("您的E币余额不够，不能与主播视频\n请及时充值", "我知道了", "立即充值"), new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                            @Override
                            public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                switch (yesOrNoType) {
                                    case BtnOk:
                                        RechargeActivity.openActivity(AnchorHomeActivity.this, RechargeActivity.class);
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }).show();
                    } else {
                        AVChatUserData avChatUserData = new AVChatUserData(
                                UserHelper.getUserId()
                                , id
                                , data.getAvatar()
                                , data.getUsername()
                                , "1"
                                , true
                                , data.getPrice()
                                , 0);
                        AVChatKit.outgoingCall(AnchorHomeActivity.this,
                                data.getMobile(),
                                data.getUsername(),
                                AVChatType.VIDEO.getValue(),
                                AVChatActivity.FROM_INTERNAL, avChatUserData);
                    }


                }
                break;
            //立即预约
            case R.id.tv_subscribe:
                if (id.equals(UserHelper.getUserId())) {
                    _showToast("不能预约自己");
                    return;
                }
                //未拨通提示 传主播头像
//                new YuyueTipDialog(v.getContext(), data.getAvatar(), new YuyueTipDialog.OnYuyueTipDialogClickListener() {
//                    @Override
//                    public void onYesClick() {
                //预约 传主播id
                new YuyueDialog(v.getContext(), id, new YuyueDialog.OnYuyueDialogClickListener() {
                    @Override
                    public void onYesClick() {

                    }
                }).show();
//                    }
//                }).show();
                break;
            case R.id.view_mask:
            case R.id.iv_close:
                view_mask.setVisibility(View.GONE);
                rl_gift.setVisibility(View.GONE);
                rl_pay.setVisibility(View.GONE);
                break;
            case R.id.bt_pay:
                payECoin(currentVideo.getId() + "");
                break;
        }
    }
}
